//
//  HYMainViewController.m
//  HYSildeslipMenu

//  Created by 上官惠阳 on 2016/12/21.
//  Copyright © 2016年 上官惠阳. All rights reserved.


#import "HYMainViewController.h"
#import "AppDelegate.h"
#import "HYLeftViewController.h"
#import "HYRigthViewController.h"
#import "HYMiddleViewController.h"
#import "HYVerCustomSlideViewController.h"
#import "HYBaseNavigationViewController.h"
//#import "NTESSessionListViewController.h"
//#import "NTESContactViewController.h"
//#import "NTESNavigationHandler.h"
//#import "NTESSettingViewController.h"
#import "LVThreeBtnTool.h"
#import "LVLeveWorldViewController.h"
#import "LVSessionManageVc.h"

//#import "LVSayHelloViewController.h"
/***  当前屏幕宽度 */
#define kScreenWidth  [[UIScreen mainScreen] bounds].size.width
/***  当前屏幕高度 */
#define kScreenHeight  [[UIScreen mainScreen] bounds].size.height
@interface HYMainViewController ()<HYVerCustomSlideViewControllerDelegate,HYVerCustomSlideViewControllerDataSource,LVThreeBtnToolDelegate>
//NIMSystemNotificationManagerDelegate,NIMConversationManagerDelegate,

@property (nonatomic, weak) HYVerCustomSlideViewController *verSlideViewController;
@property (strong,nonatomic)HYMiddleViewController *middleVc;

@property (strong,nonatomic) UINavigationController *leftNav;
@property (strong,nonatomic) UINavigationController *rigthNav;
@end

@implementation HYMainViewController
@synthesize hiddenStatusBar = _hiddenStatusBar;
@synthesize barStyle = _barStyle;

+ (instancetype)instance{
    AppDelegate *delegete = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UIViewController *vc = delegete.window.rootViewController;
    if ([vc isKindOfClass:[HYMainViewController class]]) {
        return (HYMainViewController *)vc;
    }else{
        return nil;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];

    [self setUpVerViews];

    /*
    [[NIMSDK sharedSDK].systemNotificationManager addDelegate:self];
    [[NIMSDK sharedSDK].conversationManager addDelegate:self];
    extern NSString *NTESCustomNotificationCountChanged;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onCustomNotifyChanged:) name:NTESCustomNotificationCountChanged object:nil];
     */

    //是否可滚动监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onOrOffScrollMainView:) name:@"NotificationOnOrOffScrollMainView" object:nil];

    [self createThreeBtn];
}
#pragma mark noti
- (void)onOrOffScrollMainView:(NSNotification *)noti
{
    /*是一个数组，第一个元素是代表是否可滚动,是否显示底部三个按钮，
     第二个元素代表是哪一个控制器发来的通知 0-会话界面,1-拍照界面,2-通讯录界面
     */

    NSArray *arr = [noti object];
    if (arr.count != 2) {
        return;
    }

    int indexfromVc = [arr[1] intValue];
    if (indexfromVc != self.verSlideViewController.seletedIndex) {
        return;
    }

    BOOL canScroll = [arr[0] boolValue];
    self.verSlideViewController.canScroll = canScroll;

    if(self.verSlideViewController.seletedIndex == 1){
        return;
    }
    [[LVThreeBtnTool shareThreeBtn] hideAllBtn:!canScroll];
}
#pragma mark makeUI
- (void)setUpVerViews {
    [self.verSlideViewController reloadData];

    self.verSlideViewController.seletedIndex = 2;

    self.verSlideViewController.seletedIndex = 1;
}
//创建底部三个按钮
- (void)createThreeBtn
{
    LVThreeBtnTool *tool = [LVThreeBtnTool shareThreeBtn];
    [tool  createThreeBtnAtView:self.view delegate:self];
    [tool showOtherViewUnderDirection:LVThreeBtnCenter distance:30];
}

#pragma mark LVThreeBtnToolDelegate
- (void)clickedThreeBtnTool:(LVThreeBtnTool *)threeBtnTool direction:(LVThreeBtnDirection)direction
{
    switch (direction) {
        case LVThreeBtnLeft:
        {
            self.verSlideViewController.seletedIndex = 0;
        }
            break;
        case LVThreeBtnCenter:
        {
            if (self.verSlideViewController.seletedIndex == 1 && _middleVc.verSlideVc.seletedIndex == 1) {
                LVLeveWorldViewController *leveWorldVc = [_middleVc.seletedNav.childViewControllers firstObject];
                [leveWorldVc greetingButtonPressed];
            }else{
                self.verSlideViewController.seletedIndex = 1;
                _middleVc.verSlideVc.seletedIndex = 1;
            }
        }
            break;
        case LVThreeBtnRigth:
        {
            self.verSlideViewController.seletedIndex = 2;
        }
            break;
        default:
            break;
    }
}
#pragma mark 状态栏处理
/** statusBar是否隐藏*/
- (void)setHiddenStatusBar:(BOOL)hiddenStatusBar {
    _hiddenStatusBar = hiddenStatusBar;
    //更新状态栏
    [self setNeedsStatusBarAppearanceUpdate];
}

- (BOOL)hiddenStatusBar {
    return _hiddenStatusBar;
}

- (void)setBarStyle:(UIStatusBarStyle)barStyle {
    if (_barStyle == barStyle) return ;
    _barStyle = barStyle;

    [self setNeedsStatusBarAppearanceUpdate];
}

- (BOOL)prefersStatusBarHidden {
    return self.hiddenStatusBar;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return self.barStyle;
}
#pragma mark 创建滚动视图控制器
- (HYVerCustomSlideViewController *)verSlideViewController {
    if (!_verSlideViewController) {
        HYVerCustomSlideViewController *slide = [[HYVerCustomSlideViewController alloc] init];
        slide.isTransversal = YES;
        [self addChildVc:slide];
        _verSlideViewController = slide;
        slide.delgate = self;
        slide.dataSource = self;
    }
    return _verSlideViewController;
}
#pragma mark other
- (void)addChildVc:(UIViewController *)childVc {
    if ([childVc isKindOfClass:[UIViewController class]] == NO) {
        return ;
    }
    [childVc willMoveToParentViewController:self];
    [self addChildViewController:childVc];
    [self.view addSubview:childVc.view];
    childVc.view.frame = self.view.bounds;
}
//带根视图的导航栏
- (UINavigationController *)navWithRootVc:(UIViewController *)vc
{
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
//    
//    NTESNavigationHandler *handler = [[NTESNavigationHandler alloc] initWithNavigationController:nav];
//    nav.delegate = handler;
    HYBaseNavigationViewController *nav = [[HYBaseNavigationViewController alloc] initWithRootViewController:vc];
    return nav;
}
- (UINavigationController *)seletedNav
{
    if (_seletedNav == nil) {
        _seletedNav = _middleVc.seletedNav;
    }
    return _seletedNav;
}
#pragma mark - HYVerCustomSlideViewControllerDataSource
- (UIViewController *)verSlideViewController:(HYVerCustomSlideViewController *)slideViewController viewControllerAtIndex:(NSInteger)index {
    switch (index) {
        case 0:
        {
//            NTESSessionListViewController *sessionVc = [[NTESSessionListViewController alloc] init];

            LVSessionManageVc *sessionManagerVc = [[LVSessionManageVc alloc] init];
            UINavigationController *nav = [self navWithRootVc:sessionManagerVc];
            self.leftNav = nav;

            return nav;
        }
            break;
        case 1:
        {
            HYMiddleViewController *middleVc = [[HYMiddleViewController alloc] init];
            _middleVc = middleVc;
            __weak typeof(self) weakSelf = self;
            middleVc.scrollBlock = ^(BOOL canScroll,UINavigationController *seletedNav){
                weakSelf.verSlideViewController.canScroll = canScroll;
                weakSelf.hiddenStatusBar = canScroll;
                weakSelf.seletedNav = seletedNav;
            };
            return middleVc;
        }
            break;
        case 2:
        {
            HYLeftViewController *leftVc = [[HYLeftViewController alloc] init];
            UINavigationController *nav = [self navWithRootVc:leftVc];
            self.rigthNav = nav;
            return nav;
        }
            break;
        default:
            break;
    }
    return nil;
}

- (NSInteger)numberOfChildViewControllersInVerSlideViewController:(HYVerCustomSlideViewController *)slideViewController {
    return 3;
}
#pragma mark HYVerCustomSlideViewControllerDelegate
- (void)verCustomSlideViewController:(HYVerCustomSlideViewController *)slideViewController slideOffset:(CGPoint)slideOffset
{
    //当屏幕滑过一半就要做处理了，避免出现闪动效果
    if (slideOffset.x < kScreenWidth / 2) {
        self.hiddenStatusBar = NO;
    }else if (slideOffset.x > kScreenWidth + kScreenWidth / 2){
        self.hiddenStatusBar = NO;
    }else{
        self.hiddenStatusBar = YES;
    }
}
- (void)verCustomSlideViewController:(HYVerCustomSlideViewController *)slideViewController slideIndex:(NSInteger)slideIndex {
    switch (slideIndex) {
        case 0:
        {
            self.hiddenStatusBar = NO;
            self.seletedNav = self.leftNav;

            [[LVThreeBtnTool shareThreeBtn] showInBothSides];
            [[LVThreeBtnTool shareThreeBtn] scrollToDirection:LVThreeBtnLeft];
        }
            break;
        case 1:
        {
            self.hiddenStatusBar = YES;
            self.seletedNav = _middleVc.seletedNav;

            [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationOnOrOffScrollMainView" object:@[@(YES),@(1)]];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationOnOrOffScrollMiddleView" object:@[@(YES),@(1)]];

            [[LVThreeBtnTool shareThreeBtn] showInTheMiddle];
            [[LVThreeBtnTool shareThreeBtn] scrollToDirection:LVThreeBtnCenter];
            [[LVThreeBtnTool shareThreeBtn] showOtherViewUnderDirection:LVThreeBtnCenter distance:30];
        }
            break;
        case 2:
        {
            self.hiddenStatusBar = NO;
            self.seletedNav = self.rigthNav;

            [[LVThreeBtnTool shareThreeBtn] showInBothSides];
            [[LVThreeBtnTool shareThreeBtn] scrollToDirection:LVThreeBtnRigth];
        }
        default:
            break;
    }
}
/*
#pragma mark - NIMConversationManagerDelegate
- (void)didAddRecentSession:(NIMRecentSession *)recentSession
           totalUnreadCount:(NSInteger)totalUnreadCount{

}
- (void)didUpdateRecentSession:(NIMRecentSession *)recentSession
              totalUnreadCount:(NSInteger)totalUnreadCount{

}
- (void)didRemoveRecentSession:(NIMRecentSession *)recentSession totalUnreadCount:(NSInteger)totalUnreadCount{

}

- (void)messagesDeletedInSession:(NIMSession *)session{

}

- (void)allMessagesDeleted{

}

#pragma mark - NIMSystemNotificationManagerDelegate
- (void)onSystemNotificationCountChanged:(NSInteger)unreadCount
{

}
#pragma mark - Notification
- (void)onCustomNotifyChanged:(NSNotification *)notification
{

}
*/
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
